import { AppConsts } from '@/models/AppConsts';
import storage from '@/utils/storage';
import { getAuthUrl } from '@/utils/utils';
import { createRouter, createWebHashHistory } from 'vue-router'

var routes = [
    { path: "/", component: () => import('@/views/index.vue'), meta: { title: "首页" } },
    { path: "/login", component: () => import('@/views/login/Login.vue'), meta: { anonymous: true, title: "绑定账号" } },
    { path: "/user/list", component: () => import('@/views/userManage/UserList.vue'), meta: { title: "用户列表" } },
    { path: "/user/adduser", component: () => import('@/views/userManage/AddUser.vue'), meta: { title: "添加用户" } },

    { path: "/xue/sync", component: () => import('@/views/xue/XueSync.vue'), meta: { title: "同步靴" } },
    { path: "/xue/list", component: () => import('@/views/xue/XueList.vue'), meta: { title: "靴列表" } },

    { path: "/product/list", component: () => import('@/views/productManage/ProductList.vue'), meta: { title: "链接管理" } },

];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});


router.beforeEach((to, _from, next) => {
    // next();
    // return;
    document.title = (to.meta.title as string);
    if (!to.meta.anonymous) {
        let token = storage.getStorage(AppConsts.ADMIN_USER_TKN);
        if (token) {
            next();
        } else {
            let url = to.path;
            console.log(url);
            router.replace({ path: "/login", query: { retUrl: url } });

        }
    } else {
        next();
    }
});

export default router;